<?php

namespace app\controllers\h5\uni;

use app\controllers\h5\base\H5Controller;
use app\models\Brand;
use app\models\marketing\MarketingGroup;
use app\models\order\Order;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\product\ProductCategory;
use app\models\system\SetApp;
use app\models\system\SetFlow;
use app\models\system\SetHome;
use app\models\system\SetNav;
use app\models\system\SetSwiper;
use app\redis\Cache;

/**
 * Class HomeController
 * @package app\controllers\h5\uni
 */
class HomeController extends H5Controller
{
    /**
     * uni home page
     *
     * @return \yii\web\Response
     */
    public function actionIndex()
    {
        // 腾讯控制, 支付控制
        $x = SetFlow::getLastOne();

        // $data = Cache::get(Cache::$PAGE_HOME);
        $data = null;
        if ($data == null) {
            $set = SetHome::getLastOne();
            $data = [
                'app_name' => $this->appName(),
                'category' => $this->getCategory(),
                'swiper' => $this->swiper(),
                'nav' => [
                    'data' => $this->nav(),
                    'total' => $set->nav_total
                ],
                // 'day' => $this->day($set->day),
                // 'group' => $this->group($set->group),
                // 'brand' => $this->brand($set->brand, $set->brand_total),
                'recommend' => $this->recommend($set->product),
                // 'group_quantity' => $this->getGroupQuantity(),
                'wx' => $x->wx == 1
            ];

            // Cache::set(Cache::$PAGE_HOME, json_encode($data));
        } else {
            $data = json_decode($data, true);
        }

        return $this->successEncode($data);
    }

    private function getGroupQuantity()
    {
        $set = MarketingGroup::getLastOne();
        return $set->quantity;
    }

    private function appName()
    {
        $set = SetApp::getLastOne();

        return $set->app_name;
    }

    private function getCategory()
    {
        return ProductCategory::find()
            ->where(['status' => ProductCategory::$statusMapping['OK']])
            ->andWhere(['parent_id' => 0])
            ->select(['id', 'name'])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            // 小程序首页限制数量
            ->limit(4)
            ->asArray()
            ->all();
    }

    private function swiper()
    {
        return SetSwiper::find()
            ->where(['status' => SetSwiper::$statusMapping['OK']])
            // 首页的轮播图
            ->andWhere(['category_id' => 1])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['image'])
            // ->select(['id', 'image_id'])
            ->asArray()
            ->all();
    }

    private function nav()
    {
        $list = SetNav::find()
            ->where(['status' => SetNav::$statusMapping['OK']])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['image'])
            ->asArray()
            ->all();

        // var_dump($list);exit;

        return $list;
    }

    // 大礼包商品
    private function day($total)
    {
        $category_id = 181;

        return Product::find()
            ->where(['status' => Product::$statusMapping['OK']])
            ->andWhere(['category_id' => $category_id])
            ->orderBy(['id' => SORT_DESC])
            ->limit($total)
            ->with(['mainImage'])
            ->asArray()
            ->all();
    }

    private function group($total)
    {
        $ids = ProductAreaList::find()
            ->where(['area_id' => 5])
            ->limit($total)
            ->orderBy(['id' => SORT_DESC])
            ->select(['product_id'])
            ->column();

        return Product::find()
            ->where(['status' => Product::$statusMapping['OK']])
            ->andWhere(['id' => $ids])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['mainImage'])
            ->asArray()
            ->all();
    }

    private function brand($total, $product_total)
    {
        $list = Brand::find()
            ->where(['status' => Brand::$statusMapping['OK']])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->limit($total)
            ->with(['image', 'products', 'products.mainImage'])
            ->asArray()
            ->all();

        // 处理tag, products数量
        foreach ($list as &$item) {
            $tags = str_replace('，', ',', $item['tag']);
            $tags = explode(',', $tags);
            $item['tags'] = $tags;

            // 最多只取4个商品
            if (count($item['products']) > $product_total ) {
                $item['products'] = array_slice($item['products'], 0, $product_total);
            }
        }

        return $list;

        /**
         * todo
         * 必须取出有商品 > 3 的品牌,
         * 再根据后台设置的排序输出
         */
    }

    private function recommend($total)
    {
        return Product::find()
            ->where(['status' => Product::$statusMapping['OK']])
            // 这里需要推荐, 就让显示在首页
            ->andWhere(['is_recommend' => 1])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['mainImage'])
            ->limit($total)
            ->asArray()
            ->all();
    }

    // 首页显示数量
    public function actionNum()
    {
        $count = Order::find()
            ->where(['total_pay' => 22])
            ->count();

        $set = SetFlow::getLastOne();

        return $this->successEncode(['num' => $set->recommend_num + intval($count)]);
    }
}